package cn.wokoba.trigger.listener;


import cn.wokoba.service.activity.model.OrderTradeTypeVO;
import cn.wokoba.service.activity.model.SkuRechargeEntity;
import cn.wokoba.service.activity.service.quota.RaffleActivityAccountQuotaService;
import cn.wokoba.service.credit.CreditAdjustService;
import cn.wokoba.service.credit.model.TradeEntity;
import cn.wokoba.service.credit.model.TradeNameVO;
import cn.wokoba.service.credit.model.TradeTypeVO;
import cn.wokoba.service.rabate.event.SendRebateMessageEvent;
import cn.wokoba.types.enums.ResponseCode;
import cn.wokoba.types.event.BaseEvent;
import cn.wokoba.types.exception.AppException;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
@Slf4j
public class RebateMessageCustomer {
    @Value("${spring.rabbitmq.topic.send_rebate}")
    private String topic;
    @Resource
    private RaffleActivityAccountQuotaService raffleActivityAccountQuotaService;
    @Resource
    private CreditAdjustService creditAdjustService;

    @RabbitListener(queuesToDeclare = @Queue(value = "${spring.rabbitmq.topic.send_rebate}"))
    public void listener(String message) {
        try {
            log.info("监听用户行为返利消息 topic: {} message: {}", topic, message);
            // 1. 转换消息
            BaseEvent.EventMessage<SendRebateMessageEvent.RebateMessage> eventMessage = JSON.parseObject(message, new TypeReference<BaseEvent.EventMessage<SendRebateMessageEvent.RebateMessage>>() {
            }.getType());
            SendRebateMessageEvent.RebateMessage rebateMessage = eventMessage.getData();

            // 2. 入账奖励
            switch (rebateMessage.getRebateType()) {
                case "sku" -> {
                    SkuRechargeEntity skuRechargeEntity = new SkuRechargeEntity();
                    skuRechargeEntity.setUserId(rebateMessage.getUserId());
                    skuRechargeEntity.setSku(Long.valueOf(rebateMessage.getRebateConfig()));
                    skuRechargeEntity.setOutBusinessNo(rebateMessage.getBizId());
                    skuRechargeEntity.setOrderTradeType(OrderTradeTypeVO.rebate_no_pay_trade);
                    raffleActivityAccountQuotaService.createOrder(skuRechargeEntity);
                }
                case "integral" -> {
                    TradeEntity tradeEntity = new TradeEntity();
                    tradeEntity.setUserId(rebateMessage.getUserId());
                    tradeEntity.setTradeName(TradeNameVO.REBATE);
                    tradeEntity.setTradeType(TradeTypeVO.FORWARD);
                    tradeEntity.setAmount(new BigDecimal(rebateMessage.getRebateConfig()));
                    tradeEntity.setOutBusinessNo(rebateMessage.getBizId());
                    raffleActivityAccountQuotaService.createOrder(SkuRechargeEntity.builder()
                            .sku(9010L)
                            .userId(rebateMessage.getUserId())
                            .outBusinessNo(rebateMessage.getBizId())
                            .orderTradeType(OrderTradeTypeVO.rebate_no_pay_trade)
                            .build());
                    creditAdjustService.createOrder(tradeEntity);
                }
            }
        } catch (AppException e) {
            if (ResponseCode.INDEX_DUP.getCode().equals(e.getCode())) {
                log.warn("监听用户行为返利消息，消费重复 topic: {} message: {}", topic, message, e);
                return;
            }
            throw e;
        }
    }
}

